package com.dkd.manage.service.impl;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.*;
import com.dkd.manage.domain.vo.VmGetRegionEmp;
import com.dkd.manage.domain.vo.VmInfo2;
import com.dkd.manage.domain.vo.VmInfoWithChannelAndSku;
import com.dkd.system.service.impl.SysDictDataServiceImpl;
import com.dkd.system.service.impl.SysDictTypeServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.TbVendingMachineMapper;
import com.dkd.manage.service.ITbVendingMachineService;
import org.springframework.transaction.annotation.Transactional;
/**
 * 设备Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-23
 */
@Service
public class TbVendingMachineServiceImpl implements ITbVendingMachineService {
    @Autowired
    private TbVendingMachineMapper tbVendingMachineMapper;
    @Autowired
    private TbVmTypeServiceImpl tbVmTypeServiceImpl;
    @Autowired
    private TbNodeServiceImpl tbNodeServiceImpl;
    @Autowired
    private SysDictTypeServiceImpl sysDictTypeServiceImpl;
    @Autowired
    private SysDictDataServiceImpl sysDictDataServiceImpl;
    @Autowired
    private TbChannelServiceImpl tbChannelServiceImpl;
    @Autowired
    private TbRegionServiceImpl tbRegionServiceImpl;
    @Override
    public VmInfoWithChannelAndSku selectVmWithChannelAndSkuInfo(Long id) {
        return tbVendingMachineMapper.selectVmWithChannelAndSkuInfo(id);
    }
    /**
     * 查询设备
     *
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public TbVendingMachine selectTbVendingMachineById(Long id) {
        return tbVendingMachineMapper.selectTbVendingMachineById(id);
    }
    @Override
    public VmInfo2 selectVmInfoById(Long id) {
        return tbVendingMachineMapper.selectVmInfoById(id);
    }
    /**
     * 查询设备列表
     *
     * @param tbVendingMachine 设备
     * @return 设备
     */
    @Override
    public List<TbVendingMachine> selectTbVendingMachineList(TbVendingMachine tbVendingMachine) {
        return tbVendingMachineMapper.selectTbVendingMachineList(tbVendingMachine);
    }
    /**
     * 新增设备
     *
     * @param tbVendingMachine 设备
     * @return 结果
     */
    // @Transactional 涉及操作新增，两张表，要不都成功，要不都失败。所以要添加事务回滚
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTbVendingMachine(TbVendingMachine tbVendingMachine) {
        //分两步
        //1 . 设备表：新增设备
        //在新增设备的时候，需要同时查询 点位，设备类型，这两张表，去填充冗余字段信息：
        //例如，设备【容量】，是从设备类型表里面获取，然后补充到设备表中
        TbVmType tbVmType = tbVmTypeServiceImpl.selectTbVmTypeById(tbVendingMachine.getVmTypeId());
        tbVendingMachine.setChannelMaxCapacity(tbVmType.getChannelMaxCapacity());
        //通过UUIDUtils 生成设备编号
        String innerCode = UUIDUtils.getUUID();
        tbVendingMachine.setInnerCode(innerCode);
        //查询点位表，补充 区域，点位，合作商等信息
        TbNode tbNode = tbNodeServiceImpl.selectTbNodeById(tbVendingMachine.getNodeId());
        /*tbVendingMachine.setRegionId(tbNode.getRegionId());
        tbVendingMachine.setPartnerId(tbNode.getPartnerId());
        tbVendingMachine.setNodeId(tbNode.getId());*/
        //这里可以通过BeanUtil，将tbNode里面的信息，拷贝到tbVendingMachine里面，但注意的是，有一些同名的属性，需要排除。
        //这里是忽略id
        BeanUtil.copyProperties(tbNode, tbVendingMachine, "id");
        //点位地址不能复制，因为col的名字不一样，一个是叫address，一个是叫addr，所以这里是手动赋值
        tbVendingMachine.setAddr(tbNode.getAddress());
        //设置售货机状态 : 未投放
        tbVendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);
        tbVendingMachine.setUpdateTime(DateUtils.getNowDate());
        //插入设备 要先插入设备，这样才有设备id
        Integer result = tbVendingMachineMapper.insertTbVendingMachine(tbVendingMachine);
        //2 . 货道表：新增货道
        //一个设备，有N条货道，所以需要循环，循环次数是：货道行数*货道列数
        List<TbChannel> arrayList = new ArrayList<TbChannel>();
        for (int i = 1; i <= tbVmType.getVmRow(); i++) {
            for (int j = 1; j <= tbVmType.getVmCol(); j++) {
                TbChannel tbChannel = new TbChannel();
                tbChannel.setChannelCode(i + "-" + j);
                tbChannel.setVmId(tbVendingMachine.getId());
                tbChannel.setInnerCode(tbVendingMachine.getInnerCode());
                tbChannel.setMaxCapacity(tbVmType.getChannelMaxCapacity());
                tbChannel.setCreateTime(DateUtils.getNowDate());
                tbChannel.setUpdateTime(DateUtils.getNowDate());
                //保存货道 这里做一个优化，不适用单条插入货道，而是使用批量插入货道。
                //tbChannelServiceImpl.insertTbChannel(tbChannel);
                //批量插入货道，先保存到list集合中，最后批量插入
                arrayList.add(tbChannel);
            }
        }
        //批量插入货道
        tbChannelServiceImpl.batchInsertTbChannels(arrayList);
        return result;
    }
    /**
     * 修改设备
     *
     * @param tbVendingMachine 设备
     * @return 结果
     */
    @Override
    public int updateTbVendingMachine(TbVendingMachine tbVendingMachine) {
        TbRegion tbRegion = tbRegionServiceImpl.selectTbRegionById(tbVendingMachine.getRegionId());
        TbNode tbNode = tbNodeServiceImpl.selectTbNodeById(tbVendingMachine.getNodeId());
        String addr = tbNode.getAddress();
        //更新点位详细地址
        tbVendingMachine.setAddr(addr);
        //商圈类型 区域Id 合作商Id
        BeanUtil.copyProperties(tbNode, tbVendingMachine, "id");
        tbVendingMachine.setRegionId(tbRegion.getId());
        return tbVendingMachineMapper.updateTbVendingMachine(tbVendingMachine);
    }
    /**
     * 批量删除设备
     *
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteTbVendingMachineByIds(Long[] ids) {
        return tbVendingMachineMapper.deleteTbVendingMachineByIds(ids);
    }
    /**
     * 删除设备信息
     *
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteTbVendingMachineById(Long id) {
        return tbVendingMachineMapper.deleteTbVendingMachineById(id);
    }
    @Override
    public VmGetRegionEmp getEmpWithVmRegion(Long id) {
        return tbVendingMachineMapper.getEmpWithVmRegion(id);
    }
}
